﻿using System;
using System.Configuration;
using System.Data.SqlServerCe;

namespace Dados.ConexaoCE
{
    public class CEMSTransacao : ICETransaction
    {
        private SqlCeTransaction transacao;
        private SqlCeConnection conexao;

        #region construtores

        public CEMSTransacao()
        {
            string stringconexao = @"Persist Security Info = false; 
                                           Data Source = dbProvisoria.sdf";
            conexao = new SqlCeConnection(stringconexao);
        }

        public CEMSTransacao(string strConexao)
        {
            try
            {
                string stringconexao = @"Persist Security Info = false; 
                                           Data Source = dbProvisoria.sdf";

                conexao = new SqlCeConnection(stringconexao);
            }
            catch (Exception ex)
            {                
                //Utilitarios.gravarLog(ex.Message);
                throw ex;
            }
        }

        #endregion

        #region propriedades

        public SqlCeTransaction Transacao
        {
            get { return transacao; }
        }

        public SqlCeConnection Conexao
        {
            get { return conexao; }
        }

        #endregion

        #region iniciarTransacao

        public void iniciarTransacao()
        {
            conexao.Open();
            transacao = conexao.BeginTransaction();            
        }

        #endregion

        #region salvarTransacao

        public void salvarTransacao()
        {
            transacao.Commit();
            conexao.Close();
        }

        #endregion

        #region desfazerTransacao

        public void desfazerTransacao()
        {
            transacao.Rollback();
            conexao.Close();
        }

        #endregion

    }
}
